/*
  ******************************************************************************
  * @file    RGB_demo.c
  * @author  FreqChip Firmware Team
  * @version V1.0.0
  * @date    2023
  * @brief   RGB interface module Demo.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 FreqChip.
  * All rights reserved.
  ******************************************************************************
*/
#include "driver_nv3047_rgb.h"
#include "FreeRTOS.h"
#if (BOARD_SEL == BOARD_EVB_FR3092E_RGB)

static GPIO_InitTypeDef GPIO_Handle;
PARALLEL_HandTypeDef hparallel;
SPI_HandleTypeDef    spi_handle;
DMA_HandleTypeDef dma_handle;
DMA_HandleTypeDef dma1_handle;
struct_RGB_TypeDef_t rgb_handle;

unsigned char *coply = NULL;

void timer0_irq(void);

 /************************************************************************************
 * @fn      rgb_display_controller_init
 *
 * @brief   rgb_display_controller_init
 *

 */ 
void rgb_display_controller_init(void)
{
    /* init parallel CLOCK */  
    __SYSTEM_PARALLEL_CLK_ENABLE();
    __SYSTEM_PARALLEL_CLK_SELECT_SPLL();
    __SYSTEM_GPIOA_CLK_ENABLE();
    __SYSTEM_GPIOB_CLK_ENABLE();
    __SYSTEM_GPIOC_CLK_ENABLE();
    __SYSTEM_GPIOD_CLK_ENABLE();   
    __SYSTEM_DMA0_CLK_ENABLE();
    __SYSTEM_DMA1_CLK_ENABLE();
    __SYSTEM_TIMER0_CLK_ENABLE();
    //__SYSTEM_SPI_MASTER1_X8_CLK_ENABLE();
    __SYSTEM_SPI_MASTER0_X8_CLK_ENABLE();
    printf("parallel clock:%d\r\n", system_get_peripheral_clock( PER_CLK_PARALLEL));

    /* RGB io init */ 
    /*
        D0~D15 PC0~15
        DCLK   PB13
    */
    GPIO_Handle.Alternate = GPIO_FUNCTION_8;
    GPIO_Handle.Mode      = GPIO_MODE_AF_PP;
    GPIO_Handle.Pin       = RGB565_LCD_DATA_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_DATA_PORT, &GPIO_Handle);  
    
    GPIO_Handle.Alternate = GPIO_FUNCTION_8;
    GPIO_Handle.Mode      = GPIO_MODE_AF_PP;
    GPIO_Handle.Pin       = RGB565_LCD_DCLK_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_DCLK_PORT, &GPIO_Handle);

	GPIO_Handle.Alternate = GPIO_FUNCTION_8;
    GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_VSYNC_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_VSYNC_PORT, &GPIO_Handle);
	
	GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_HSYNC_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_HSYNC_PORT, &GPIO_Handle);
	
	GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_DE_EN_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_DE_EN_PORT, &GPIO_Handle);
	
	GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_RESET_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_RESET_PORT, &GPIO_Handle);
	
    GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = GPIO_PIN_13;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(GPIOD, &GPIO_Handle);
    rgb_lcd_reset_release();

    /* backlight */ 
    GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_BACKLIGHT_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_BACKLIGHT_PORT, &GPIO_Handle); 
	rgb_lcd_backlight_set();
    rgb_lcd_disp_set();//Display control / standby mode selection. Internal pull low.DISP = “Low” : Standby.DISP = “High” : Normal display.
    system_delay_us(1000 * 20);

	#ifdef RGB56_LCD_INIT_CONFIG
    /* SPI CS */ 
    GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_SPI_CS_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_SPI_CS_PORT, &GPIO_Handle);
	
	    /* SPI io init */
    // B0,B2 B3
    GPIO_Handle.Alternate = GPIO_FUNCTION_7;
    GPIO_Handle.Mode      = GPIO_MODE_AF_PP;
    GPIO_Handle.Pin       = RGB565_LCD_SPI_CLK_GPIO|RGB565_LCD_SPI_MOSI_GPIO|RGB565_LCD_SPI_MISO_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_SPI_CLK_PORT, &GPIO_Handle);  
	
	 /* SPI init */
    spi_handle.SPIx                       = SPIMX8_0;
    spi_handle.Init.Work_Mode             = SPI_WORK_MODE_3;
    spi_handle.Init.Frame_Size            = SPI_FRAME_SIZE_9BIT;
    spi_handle.Init.BaudRate_Prescaler    = 100;
    spi_handle.Init.TxFIFOEmpty_Threshold = 0;
    spi_handle.Init.RxFIFOFull_Threshold  = 0;
    spi_master_init(&spi_handle);
	#endif
    /* PARALLEL Init */
    hparallel.PARALLELx = PARALLEL0;
    hparallel.Init.DataBusSelect = DATA_BUS_16_BIT;
    hparallel.Init.ParallelMode  = MODE_6800;
    hparallel.PARALLELx->DATA_CFG.DATA_TRANS_SEQ_0 = 0;
    hparallel.PARALLELx->DATA_CFG.DATA_TRANS_SEQ_1 = 1;
    hparallel.PARALLELx->DATA_CFG.DATA_TRANS_SEQ_2 = 2;
    hparallel.PARALLELx->DATA_CFG.DATA_TRANS_SEQ_3 = 3;
    hparallel.Init.ReadClock  = WDCLK_DIV_4;
    hparallel.Init.WriteClock = WDCLK_DIV_2;
    parallel_init(&hparallel);
    hparallel.PARALLELx->CRM.WR_L_LEN = 2;
    hparallel.PARALLELx->CRM.WR_H_LEN = 2;   
    __PARALLEL_CS_SET(hparallel.PARALLELx);

    /* DMA Init */
    system_dmac_request_id_config(PARALLEL_INTERFACE,DMA0_REQUEST_ID_3);    
    dma_handle.DMAx                       = DMA0;
    dma_handle.Channel                    = DMA_Channel2;
    dma_handle.Init.Data_Flow             = DMA_M2P_DMAC;
    dma_handle.Init.Request_ID            = DMA0_REQUEST_ID_3;
    dma_handle.Init.Source_Master_Sel     = DMA_AHB_MASTER_3;
    dma_handle.Init.Desination_Master_Sel = DMA_AHB_MASTER_1;
    dma_handle.Init.Source_Inc            = DMA_ADDR_INC_INC;
    dma_handle.Init.Desination_Inc        = DMA_ADDR_INC_NO_CHANGE;
    dma_handle.Init.Source_Width          = DMA_TRANSFER_WIDTH_32;
    dma_handle.Init.Desination_Width      = DMA_TRANSFER_WIDTH_32;
    dma_handle.Init.Source_Burst_Len      = DMA_BURST_LEN_16;
    dma_handle.Init.Desination_Burst_Len  = DMA_BURST_LEN_16;
    dma_init(&dma_handle);

}

 /************************************************************************************
 * @fn      rgb_display_init
 *
 * @brief   rgb_display_init
 *

 */
void rgb_display_init(void* buffer)
{
    coply = buffer;

    /* rgb init */
    rgb_init();

    NVIC_ClearPendingIRQ(TIMER0_IRQn);
    NVIC_EnableIRQ(TIMER0_IRQn);
    NVIC_ClearPendingIRQ(DMA0_IRQn);
    NVIC_EnableIRQ(DMA0_IRQn);

    rgb_display_start(Timer0, &rgb_handle, 2, coply);

    printf("%s:%d\r\n", __func__, __LINE__);
}

void timer0_irq(void)
{
	
 
	rgb_timer_IRQHandler(Timer0, &rgb_handle);
 
 
    
}

__RAM_CODE void rgb_display_dma_irq(void)
{
 
    if (dma_get_tfr_Status(&dma_handle))
    {
		 
			rgb_dma_IRQHandler(&rgb_handle);
		 
			
		dma_clear_tfr_Status(&dma_handle);
		 
    }
}
#endif
